package com.once;

import android.os.Bundle;
import android.os.Environment;
import android.text.TextUtils;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.hjq.toast.ToastUtils;
import com.once.databinding.ActivityDetailsBinding;
import com.once.db.DaoSession;

import java.io.File;
import java.text.DecimalFormat;
import java.text.MessageFormat;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Locale;

import static com.once.GuigeActivity.txt_norms;

/**
 * @author 张成
 */
public class Details extends AppCompatActivity {

    ArrayList<Double> list = new ArrayList<>();
    int curr = 1;
    private DaoSession daoSession;
    private ActivityDetailsBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityDetailsBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());
        daoSession = ((MyApplication) getApplication()).getDaoSession();
//        initFile();
        initList();
    }

    private void initList() {
        list.clear();
        List<POJO> dojos = daoSession.queryRaw(POJO.class, "where category=? and company=? and norms=?",
                MainActivity.txt_category, MainActivity.txt_company, GuigeActivity.txt_norms);
        if (dojos.size() > 0) {
            list = (ArrayList<Double>) JSON.parseArray(dojos.get(0).getWeight(), Double.class);
        }

    }

    @Override
    protected void onStart() {
        super.onStart();
        initData();
    }

    private void initFile() {
        File file = new File(Environment.getExternalStorageDirectory() + "/1仓库管理");
        if (!file.exists()) {
            boolean mkdirs = file.mkdirs();
        }
    }

    private void initData() {
        binding.guigeDet.setText(txt_norms);
        try {
            binding.xinxi.setText(MessageFormat.format("{0}件 {1}KG", list.size(), getsum()));
            binding.bianhao.setText(String.valueOf(curr));
            binding.kg.setText(String.valueOf(list.get(curr - 1)));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public void next(View view) {
        String weightTemp = binding.kg.getText() + "";
        if (TextUtils.isEmpty(weightTemp)) {
            ToastUtils.show("失败,重量不可为空！");
            return;
        }

        double weight = Double.parseDouble(weightTemp);
        if (curr - 1 < list.size()) {
            list.set(curr - 1, weight);
            try {
                binding.kg.setText(String.valueOf(list.get(curr)));
            } catch (Exception e) {
                e.printStackTrace();
                binding.kg.setText("");
            }
        } else {
            list.add(weight);
            binding.kg.setText("");
        }
        add();
        curr++;
        binding.xinxi.setText(MessageFormat.format("{0}件 {1}KG", list.size(), new DecimalFormat("0.00").format(getsum())));
        binding.bianhao.setText(String.valueOf(curr));

    }

    private void add() {
        POJO dojo = new POJO();
        List<POJO> dojos = daoSession.queryRaw(POJO.class, "where category=? and company=? and norms=?",
                MainActivity.txt_category, MainActivity.txt_company, GuigeActivity.txt_norms);
        if (dojos.size() > 0) {
            dojo = dojos.get(0);
            dojo.setWeight(JSONArray.toJSONString(list));
        } else {
            dojo.setWeight(JSONArray.toJSONString(list));
            dojo.setCompany(MainActivity.txt_company);
            dojo.setCategory(MainActivity.txt_category);
            dojo.setNorms(GuigeActivity.txt_norms);
            dojo.setTime(new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA).format(new Date()));
        }
        daoSession.insertOrReplace(dojo);

    }

    private double getsum() {
        double temp = 0;
        for (Double aDouble : list) {
            temp += aDouble;
        }
        return temp;
    }


    public void pre(View view) {
        if (curr < 2 || list.size() == 0) {
            return;
        }
        curr--;
        binding.bianhao.setText(String.valueOf(curr));
        binding.kg.setText(String.valueOf(list.get(curr - 1)));
    }


    public void wancheng(View view) {
        if (list.size() == 0) {
            ToastUtils.show("数据为空！！未添加");
        }
        finish();
    }
}
